package com.mingtu.guards.activity;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.fragment.ScreenControlFragment1;
import com.mingtu.guards.fragment.ScreenControlFragment2;

import java.util.ArrayList;

import butterknife.BindView;

public class ScreenControlActivity extends MyBaseActivity {
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.common_tab)
    CommonTabLayout commonTab;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"按设备区分", "按指标区分"};
    private int position = 0;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_screen_control;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("户外屏控制");
    }

    @Override
    protected void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        ScreenControlFragment1 screenControlFragment1 = new ScreenControlFragment1();
        ScreenControlFragment2 screenControlFragment2 = new ScreenControlFragment2();

        mFragments.add(screenControlFragment1);
        mFragments.add(screenControlFragment2);

        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(onPageChangeListener);

        commonTab.setTabData(mTabEntities);
        commonTab.setOnTabSelectListener(new MyOnTabSelectListener());
        setTextBold(0);

    }

    @Override
    protected void getData() {

    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            viewPager.setCurrentItem(position);
        }

        @Override
        public void onTabReselect(int position) {

        }
    }


    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            commonTab.setCurrentTab(position);
            setTextBold(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**
     * tab选中加粗
     *
     * @param position
     */
    private void setTextBold(int position) {
        for (int i = 0; i < commonTab.getTabCount(); i++) {
            TextView textView = commonTab.getTitleView(i);
            if (position == i) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.sp_14));
                textView.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.sp_13));
                textView.setTypeface(Typeface.DEFAULT);
            }
        }
    }


}